<template>
  <div class="image-cropper">
    <div class="cropper-container">
      <h1>图片裁切</h1>
      <div class="cropper-content">
        <div class="cropper-left">
          <el-upload
            class="upload-area"
            action="#"
            :auto-upload="false"
            :on-change="handleFileChange"
            :show-file-list="false"
          >
            <el-button type="primary">
              <el-icon><Upload /></el-icon>
              上传图片
            </el-button>
          </el-upload>
          <div class="preview"></div>
        </div>
        <div class="cropper-right">
          <div id="image-container"></div>
        </div>
      </div>
      <div class="button-group">
        <el-button type="primary" @click="cropImage">
          <el-icon><Crop /></el-icon>
          裁切
        </el-button>
        <el-button type="success" @click="downloadImage" :disabled="!croppedImageUrl">
          <el-icon><Download /></el-icon>
          下载
        </el-button>
        <el-button type="warning" @click="goToHomepage">
          <el-icon><Back /></el-icon>
          返回
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Upload, Crop, Download, Back } from '@element-plus/icons-vue'
import 'cropperjs/dist/cropper.css'
import Cropper from 'cropperjs'

const router = useRouter()
const cropper = ref(null)
const croppedImageUrl = ref('')

const handleFileChange = (file) => {
  const reader = new FileReader()
  reader.onload = (e) => {
    const image = document.createElement('img')
    image.src = e.target.result
    image.onload = () => {
      if (cropper.value) {
        cropper.value.destroy()
      }
      const imageContainer = document.getElementById('image-container')
      imageContainer.innerHTML = ''
      imageContainer.appendChild(image)
      cropper.value = new Cropper(image, {
        aspectRatio: 1,
        viewMode: 1,
        preview: '.preview',
        background: false,
        autoCropArea: 1
      })
    }
  }
  reader.readAsDataURL(file.raw)
}

const cropImage = () => {
  if (cropper.value) {
    const canvas = cropper.value.getCroppedCanvas()
    croppedImageUrl.value = canvas.toDataURL('image/png')
    ElMessage.success('图片裁切成功！')
  } else {
    ElMessage.error('请先上传图片')
  }
}

const downloadImage = () => {
  if (croppedImageUrl.value) {
    const link = document.createElement('a')
    link.href = croppedImageUrl.value
    link.download = 'cropped-image.png'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}

const goToHomepage = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.image-cropper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 10px;

  .cropper-container {
    max-width: 1000px;
    width: 98%;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
    padding: 25px;
    box-sizing: border-box;

    h1 {
      text-align: center;
      color: #4a5568;
      font-size: 2.5em;
      margin: 0 0 25px 0;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    }

    .cropper-content {
      display: flex;
      gap: 20px;
      margin-bottom: 20px;

      .cropper-left {
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 20px;

        .preview {
          width: 200px;
          height: 200px;
          border: 1px solid #ddd;
          border-radius: 8px;
          overflow: hidden;
          background-color: #f8f9fa;
        }
      }

      .cropper-right {
        flex: 2;

        #image-container {
          width: 100%;
          height: 400px;
          border: 2px dashed #ddd;
          border-radius: 8px;
          overflow: hidden;
          background-color: #f8f9fa;

          img {
            max-width: 100%;
            max-height: 100%;
          }
        }
      }
    }

    .button-group {
      display: flex;
      justify-content: center;
      gap: 15px;

      .el-button {
        min-width: 120px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;

        .el-icon {
          margin-right: 6px;
        }

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .image-cropper {
    .cropper-container {
      padding: 15px;

      h1 {
        font-size: 24px;
        margin-bottom: 20px;
      }

      .cropper-content {
        flex-direction: column;

        .cropper-left {
          .preview {
            width: 150px;
            height: 150px;
          }
        }

        .cropper-right {
          #image-container {
            height: 300px;
          }
        }
      }

      .button-group {
        flex-direction: row;
        gap: 8px;

        .el-button {
          padding: 8px 12px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>
